﻿/*
 * Author:xmfdsh 11届网络部--邓世豪
 * DataTime：2014-6-9
 * Copyright © 2014 大学生勤工助学服务队 Powered by xmfdsh & 四小狼工作室.
 * 版权所有，违者必究
 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace QGZX.Helper
{
    public class Collect
    {
        #region 模拟post，获取结果,到指定网址，获取课表数据 + GetJsonClass(string url, string data)
        /// <summary>
        /// 模拟post，获取结果,到指定网址，获取课表数据
        /// </summary>
        /// <param name="url">post的网址</param>
        /// <param name="data">post的数据</param>
        /// <returns></returns>
        public static string GetJsonClass(string url, string data)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.CookieContainer = new CookieContainer();
            request.Accept = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.Headers["Accept-Language"] = "zh-CN,zh;q=0.";
            request.Headers["Accept-Charset"] = "GBK,utf-8;q=0.7,*;q=0.3";
            request.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322;.NET CLR 2.0.50727)";
            request.KeepAlive = true;
            //上面的http头看情况而定，但是下面俩必须加  
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";

            Encoding encoding = Encoding.GetEncoding("GB2312");//根据网站的编码自定义  

            byte[] postData = encoding.GetBytes(data);
            request.ContentLength = postData.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(postData, 0, postData.Length);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();

            StreamReader streamReader = new StreamReader(responseStream, encoding);
            string GetJson = streamReader.ReadToEnd();

            streamReader.Close();
            responseStream.Close();

            return GetJson;
        } 
        #endregion
    }
}
